package com.xpec.c4.game.login;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.bank.message.BankRequest;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.message.SSessionCloseNotify;
import com.xpec.c4.game.login.async2.BankCreatePasswordGetAccountAsync;
import com.xpec.c4.game.login.async2.CharacterLoginGetAccountAsync;
import com.xpec.c4.game.login.async2.UpdateLockPlayerAndDeviceTokenGetAccountAsync;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.game.utility.wordfilter.WordFilters;
import com.xpec.c4.message.ConstantCode;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.message.login.CAccountCharacterLoginRequest;
import com.xpec.c4.message.login.CUpdateLastLockPlayerAndDeviceTokenRequest;
import com.xpec.c4.message.login.SAccountLoginRequest;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.annotations.Name;
import com.xpec.c4.service.common.BaseMessageAction;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.contexts.ScopeType;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.ServerProperties;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;
import com.xpec.c4.service.module.gateway.SetSessionStatus;
import com.xpec.c4.service.module.h5gateway.H5SetSessionStatus;


/**
 * 登录消息处理
 * 
 * @author lipeilin
 * 
 */
@Name(scopeType=ScopeType.SESSION)
public class LoginAction extends BaseMessageAction{

	private static final Log log = LogFactory.getLog(LoginAction.class);

	/**
	 * 随机取名时刻记录<br>
	 * 
	 * @author suyinglong
	 * @version 2012-2-23 下午07:16:27
	 */
//	private long randomMoment;
	

	@MessageType(typeCode = MessageTypes.S_ACCOUNT_LOGIN_REQUEST)
	public void accountLoginRequest(SAccountLoginRequest message) {
		
		// login 收到 gateway 轉送過來的登入請求
		if (log.isInfoEnabled()) {
			log.info(LoginModule.LOGIN_TRACE_LOG_STRING+" [0]accountLoginRequest: accountID:" + message.account + " sessionID:" + sessionID);
		}
		
		LoginModule module = (LoginModule)Contexts.getModule();
		if(module != null){
			module.accountLoginRequest(message, sessionID);
		}
	}
	
	/**
	 * 
	 * @param message
	 */
	@MessageType(typeCode = MessageTypes.C_ACCOUNT_CHARACTER_LIST_REQUEST)
	public void characterListRequest(Object message) {
		if (log.isInfoEnabled()) {
			log.info(LoginModule.LOGIN_TRACE_LOG_STRING+" [1]characterListRequest,sessionID:" + sessionID + " | accountID:" + accountID);
		}

		// TODO kevin jb verify
		if(Contexts.getModule() != null){
			((LoginModule)Contexts.getModule()).characterListRequest(accountID, sessionID);
		}
		
	}
	
	/**
	 * 角色登录函数<br>
	 * 这里不能修改角色数据.
	 * 要修改,请到Player.loginServer()
	 * 
	 * @param message
	 *
	 * @2011-12-9 下午05:18:42
	 */
	@MessageType(typeCode = MessageTypes.C_ACCOUNT_CHARACTER_LOGIN_REQUEST)
	public void characterLoginRequest(CAccountCharacterLoginRequest message) {
		if (Contexts.getModule() != null && message != null && message.playerGuid != null) {
			if (log.isInfoEnabled()) {
				log.info(LoginModule.LOGIN_TRACE_LOG_STRING+" [3]characterListRequest,sessionID:" + sessionID + " | accountID:" + accountID
						+ " | char guid:" + message.playerGuid);
			}

			// server 已經進入關服維護狀態
			if (Contexts.getModule().isWaitToClose()) {
				LoginSender.sendAccountMessage(sessionID, ErrorCode.GAME_SERVER_WAIT_TO_CLOSE);
				return;
			}

			CharacterLoginGetAccountAsync task = new CharacterLoginGetAccountAsync();
			task.initial(accountID);
			task.setSessionID(sessionID);
			task.setAccountID(accountID);
			task.setMessage(message);

			Contexts.getModule().addAsyncCommand(task);
			String moduleId = PlayerCommonInfoManager.getInstance().getGatewayBySessionId(sessionID);
			if (GameUtility.isHaveString(moduleId)) {
				sendSetGatewaySession(sessionID, moduleId);
			}
		}
	}
	
	public void sendSetGatewaySession(GUID sessionID, String moduleName) {
		if (ServerProperties.isTargetModule(moduleName, ModuleName.H5GATEWAY)) {
			H5SetSessionStatus data = new H5SetSessionStatus();
			data.guid = sessionID;
			data.status = H5SetSessionStatus.READY;
			
			GroupMessage msg = new GroupMessage();
			msg.setMessageType(MessageTypes.S_SET_H5GATEWAY_SESSION);
			msg.setMessageContent(data);
			String moduleId = PlayerCommonInfoManager.getInstance().getGatewayBySessionId(sessionID);
			msg.setDestModuleID((moduleId != null) ? moduleId: ModuleName.H5GATEWAY);
			MessageAction.sendGroupMessage(msg);
		} else {
			// 向Gateway說明, player當前的狀態.
			SetSessionStatus data = new SetSessionStatus();
			data.guid = sessionID;
			// 先定義一個非0的數值
			data.status = SetSessionStatus.READY;
			
			GroupMessage msg = new GroupMessage();
			msg.setMessageType(MessageTypes.S_SET_GATEWAY_SESSION);
			msg.setMessageContent(data);
			String moduleId = PlayerCommonInfoManager.getInstance().getGatewayBySessionId(sessionID);
			msg.setDestModuleID((moduleId != null) ? moduleId: ModuleName.GATEWAY);
			MessageAction.sendGroupMessage(msg);
		}
	}
	
	@MessageType(typeCode = MessageTypes.S_SESSION_CLOSE_NOTIFY)
	public void recvSessionCloseNotify(SSessionCloseNotify notify) {
		LoginModule module = (LoginModule) Contexts.getModule();
		module.recvSessionCloseNotify(notify);
	}

	@MessageType(typeCode = MessageTypes.C_BANK_CREATE_PASSWORD_REQUEST)
	public void createPassword(BankRequest request){
		LoginModule module = (LoginModule)Contexts.getModule();
		
		BankCreatePasswordGetAccountAsync task = new BankCreatePasswordGetAccountAsync();
		task.initial(accountID);
		task.setSessionID(sessionID);
		task.setRequest(request);
		
		module.addAsyncCommand(task);
	}
	
	@MessageType(typeCode = MessageTypes.C_UPDATE_LOCK_PLAYER_AND_DEVICE_TOKEN_REQUEST)
	public void updateLockPlayerAndDeviceToken(CUpdateLastLockPlayerAndDeviceTokenRequest request){
		LoginModule module = (LoginModule) Contexts.getModule();
		
		UpdateLockPlayerAndDeviceTokenGetAccountAsync task = new UpdateLockPlayerAndDeviceTokenGetAccountAsync();
		task.initial(accountID);
		task.setRequest(request);
		task.setSessionID(sessionID);
		
		module.addAsyncCommand(task);
	}
	
	@MessageType(typeCode = MessageTypes.S_LOGIN_RELOAD_DB_PLAYER_NAME_FILTER)
	public void recvCharacterLoginNotify(long currentTime) {
		if (log.isInfoEnabled()) {
			log.info("recv "
					+ ConstantCode.getName(MessageTypes.class, MessageTypes.S_LOGIN_RELOAD_DB_PLAYER_NAME_FILTER));
		}

		WordFilters.reloadDbPlayerNameFilter(currentTime);
	}
	
	/**
	 * 收到 client 心跳消息要求<br>
	 * server 收到不做任何處理, 目的只是讓 client 不會在角色列表發呆時因為長時間沒有消息進出而被 proxy 斷線
	 * 
	 * @param request
	 */
	@MessageType(typeCode = MessageTypes.C_PERIOD_SYNC_REQUEST)
	public void recvPeriodSyncRequest(long clientProcessTime) {
	}
}
